$(function () {

    getUserInfo()

    $('#btn').on('click', function () {

        //eg2
        layer.confirm('确认要退出吗?', function (index) {
            location.href = '/login.html'
            localStorage.removeItem('token')
            layer.close(index);
        });


    })


})

function getUserInfo() {

    axios({
        method: 'GET',
        url: '/my/userinfo'

    }).then(function (res) {
        console.log(res.data.data);
        if (res.data.status !== 0) {
            return layui.layer.msg(res.data.message)
        }

        fn(res.data.data)

    })

}


function fn(user) {

    let name = user.nickname || user.username
    $('.welcome').html(name)

    if (user.user_pic !== null) {

        $('.avatar-text').hide()
        $('.layui-nav-img').show().attr('src', user.user_pic)

    } else {
        $('.layui-nav-img').hide()
        let first = name[0].toUpperCase()
        $('.avatar-text').show().html(first)


    }



}